﻿using System;
using System.Net;
using System.Windows;
using System.ComponentModel;
using System.Collections.Generic;

namespace BlueRoseGames.Controls.Text
{
    public class PropertyChangedDependencyObject : DependencyObject, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private static Dictionary<DependencyProperty, string> props = new Dictionary<DependencyProperty, string>();

        protected static DependencyProperty Register(string name, Type propertyType, Type ownerType, object defaultValue)
        {
            return Register(name, propertyType, ownerType, defaultValue, OnPropertyChanged);
        }

        protected static DependencyProperty Register(string name, Type propertyType, Type ownerType, object defaultValue, PropertyChangedCallback callback)
        {
            DependencyProperty prop = DependencyProperty.Register(name, propertyType, ownerType, new PropertyMetadata(defaultValue, callback));
            props.Add(prop, name); // Need to track this ourselves since DependencyProperty.Name is Internal
            return prop;
        }

        protected static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var pcdo = d as PropertyChangedDependencyObject;
            if (pcdo != null)
            {
                DependencyProperty p = e.Property;
                string propertyName = props.ContainsKey(p) ? props[p] : "Unknown";
                if (pcdo.PropertyChanged != null)
                    pcdo.PropertyChanged(p, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
